using System.Collections.Generic;
using System.Linq;
using AgileBoard.Core;

namespace AgileBoard.Web.Controllers.ViewModel
{
    public class IterationIndexView
    {
        internal IterationIndexView()
        {
            Tabs = new List<KeyValuePair<string, string>>();
        }

        public IList<KeyValuePair<string , string>> Tabs { get; private set; }

        public static IterationIndexView Create(IList<Iteration> iterations)
        {
            return new IterationIndexView {
                Tabs = BuildOrderedTabs(iterations)
            };
        }

        private static IList<KeyValuePair<string , string>> BuildOrderedTabs(IList<Iteration> iterations)
        {
            var acumulator = iterations.Count;
            
            return iterations
                .OrderByDescending(x => x.Id)
                .Select(iteration => IterationTab(iteration, acumulator--))
                .ToList();
        }

        private static KeyValuePair<string , string> IterationTab(Iteration iteration, int acumulator)
        {
            return new KeyValuePair<string, string>("iterations/show/" + iteration.Id, "Iteration " + acumulator);
        }
    }
}